<template>
  <div class="box">
    <p class="p_title">
      <el-button type="text" style="float: left;color: #000;font-size: 16px">添加单位权限信息</el-button>
    </p>
    <div style="margin-bottom: 30px;border-bottom: 2px solid rgb(242 242 242)">
      <el-row style="text-align: center;line-height: 40px;line-height: 40px;margin-bottom: 20px">
        <el-col :span="2"><span>模板类型</span></el-col>
        <el-col :span="6">
          <el-select v-model="value" placeholder="请选择模板类型" style="width: 100%" @change="getInformation({type:value})">
            <el-option
              v-for="item in options"
              :key="item.type"
              :label="item.typeName"
              :value="item.type">
            </el-option>
          </el-select>
        </el-col>
      </el-row>
      <el-row style="text-align: center;line-height: 40px;line-height: 40px;margin-bottom: 20px">
        <el-col :span="2"><span>模板名称</span></el-col>
        <el-col :span="6">
          <el-select v-model="name" placeholder="请选择模板名称" style="width: 100%"
                     @change="getTemplateInformation({names:name})">
            <el-option
              v-for="item in options1"
              :key="item.name"
              :label="item.name"
              :value="item.name">
            </el-option>
          </el-select>
        </el-col>
      </el-row>
    </div>
    <el-form>
      <p class="p-title">系统管理权限</p>
      <el-row style="border: 1px solid rgb(242 242 242);padding: 10px">
        <el-col :offset="2" :span="21">
          <el-checkbox-group v-model="roleListBox" @change="roleListEvent(roleListBox)" disabled>
            <el-checkbox
              v-for="data in roleList"
              :label="data.id"
              :key="data.id">
              {{data.name}}
            </el-checkbox>
          </el-checkbox-group>
        </el-col>
      </el-row>
      <p class="p-title">项目信息权限</p>
      <el-row style="border: 1px solid rgb(242 242 242);padding: 10px">
        <el-col :offset="2" :span="21">
          <el-checkbox-group v-model="checked" @change="listEvent(checked)">
            <el-checkbox
              v-for="data in Box1"
              :label="data.id"
              :key="data.id">
              {{data.name}}
            </el-checkbox>
          </el-checkbox-group>
        </el-col>
      </el-row>
      <p class="p-title">设计方案权限</p>
      <el-row style="border: 1px solid rgb(242 242 242);padding: 10px">
        <el-col :offset="2" :span="21">
          <el-checkbox-group v-model="checked" @change="listEvent(checked)">
            <el-checkbox
              v-for="data in Box2"
              :label="data.id"
              :key="data.id">
              {{data.name}}
            </el-checkbox>
          </el-checkbox-group>
        </el-col>
      </el-row>
      <p class="p-title">施工许可权限</p>
      <el-row style="border: 1px solid rgb(242 242 242);padding: 10px">
        <el-col :offset="2" :span="21">
          <el-checkbox-group v-model="checked" @change="listEvent(checked)">
            <el-checkbox
              v-for="data in Box3"
              :label="data.id"
              :key="data.id">
              {{data.name}}
            </el-checkbox>
          </el-checkbox-group>
        </el-col>
      </el-row>
      <p class="p-title">竣工验收权限</p>
      <el-row style="border: 1px solid rgb(242 242 242);padding: 10px">
        <el-col :offset="2" :span="21">
          <el-checkbox-group v-model="checked" @change="listEvent(checked)">
            <el-checkbox
              v-for="data in Box4"
              :label="data.id"
              :key="data.id">
              {{data.name}}
            </el-checkbox>
          </el-checkbox-group>
        </el-col>
      </el-row>
      <p class="p-title">咨询服务权限</p>
      <el-row style="border: 1px solid rgb(242 242 242);padding: 10px">
        <el-col :offset="2" :span="21">
          <el-checkbox-group v-model="checked" @change="listEvent(checked)">
            <el-checkbox
              v-for="data in Box5"
              :label="data.id"
              :key="data.id">
              {{data.name}}
            </el-checkbox>
          </el-checkbox-group>
        </el-col>
      </el-row>
      <p class="p-title">掘占路等咨询</p>
      <el-row style="border: 1px solid rgb(242 242 242);padding: 10px">
        <el-col :offset="2" :span="21">
          <el-checkbox-group v-model="checked" @change="listEvent(checked)">
            <el-checkbox
              v-for="data in Box6"
              :label="data.id"
              :key="data.id">
              {{data.name}}
            </el-checkbox>
          </el-checkbox-group>
        </el-col>
      </el-row>
      <p class="p-title">竣工验收提前查看</p>
      <el-row style="border: 1px solid rgb(242 242 242);padding: 10px">
        <el-col :offset="2" :span="21">
          <el-checkbox-group v-model="checked" @change="listEvent(checked)">
            <el-checkbox
              v-for="data in Box7"
              :label="data.id"
              :key="data.id">
              {{data.name}}
            </el-checkbox>
          </el-checkbox-group>
        </el-col>
      </el-row>
      <p class="p-title">事中事后监管权限</p>
      <el-row style="border: 1px solid rgb(242 242 242);padding: 10px">
        <el-col :offset="2" :span="21">
          <el-checkbox-group v-model="checked" @change="listEvent(checked)">
            <el-checkbox
              v-for="data in Box8"
              :label="data.id"
              :key="data.id">
              {{data.name}}
            </el-checkbox>
          </el-checkbox-group>
        </el-col>
      </el-row>
      <p class="p-title">掘占路等许可权限</p>
      <el-row style="border: 1px solid rgb(242 242 242);padding: 10px">
        <el-col :offset="2" :span="21">
          <el-checkbox-group v-model="checked" @change="listEvent(checked)">
            <el-checkbox
              v-for="data in Box9"
              :label="data.id"
              :key="data.id">
              {{data.name}}
            </el-checkbox>
          </el-checkbox-group>
        </el-col>
      </el-row>
    </el-form>
    <div style="height: 30px;line-height: 30px;width: 50%;margin: 40px 0">
      <el-button type="primary" @click="submitAddRole" style="float: right">保存</el-button>
    </div>

  </div>
</template>

<script>
  import {$aos} from '../../../store/state';
  import {http} from '../../../assets/commonjs/axios';

  export default {
    name: "templateEdit",
    data() {
      return {
        orgId: '',
        options: [],
        options1: [],
        value: '',
        name: '',
        roleListBox: [],
        roleList: [],
        //1.项目信息 2.设计方案 3.施工许可 4.竣工验收 5.技术咨询 6.监管意见
        Box1: [],
        Box2: [],
        Box3: [],
        Box4: [],
        Box5: [],
        Box6: [],
        Box7: [],
        Box8: [],
        Box9: [],
        checked: [],
        orgIdsList: '',
        orgIdsListArr: '',
      }
    },
    created() {
      $aos.getItem('orgIdList').then(res => {
        console.log(res)
        this.orgId = this.$route.query.orgId;
        this.orgIdsList = res;
        this.orgIdsListArr = res.split(',');
        console.log(this.orgIdsListArr)
        this.getModil();
        this.getRoleAll();
        if(!this.orgId && this.orgIdsListArr.length === 1 ){
          this.orgId = this.orgIdsListArr[0];
        }
        if(this.orgId || this.orgIdsListArr.length === 1){
          this.seeqxEvent();
        }
      });


    },
    methods: {
      getInformation(value) {
        http('post', 'permission/operate_model', {
          applyType: 5,
          type: value.type || this.value,
        }).then(res => {
          this.options1 = res.data.data;
        })
      },
      getTemplateInformation(value) {
        http('post', 'permission/operate_model', {
          applyType: 5,
          type: this.value,
          name: value.names || this.name
        }).then(res => {
          let directoryRights = res.data.data[0].directoryRights;
          let operationRights = res.data.data[0].operationRights;
          let Array = [];
          let Brr = [];
          if (directoryRights !== '') {
            let directory = directoryRights.split(',');
            directory.forEach((item, index) => {
              Array.push(parseInt(item));
            })
          }
          if (operationRights !== '') {
            let operation = operationRights.split(',');
            operation.forEach((item, index) => {
              Brr.push(parseInt(item))
            })
          }
          this.checked = Brr;
          this.roleListBox = Array;
        })
      },
      getModil() {
        http('post', 'permission/list_model_type',).then(res => {
          if (res.data.success) {
            this.options = res.data.data;
            console.log(this.options)
          }
        })
      },
      getRoleAll() {
        http('post', 'permission/list_rights',).then(res => {
          if (res.data.success) {
            this.roleList = res.data.data.dirRights;
            let box = res.data.data.opeRights;
            box.forEach((item) => {
              if (item.type === 1) {
                this.Box1.push(item)
              } else if (item.type === 2) {
                this.Box2.push(item)
              } else if (item.type === 3) {
                this.Box3.push(item)
              } else if (item.type === 4) {
                this.Box4.push(item)
              } else if (item.type === 5) {
                this.Box5.push(item)
              } else if (item.type === 6) {
                this.Box6.push(item)
              } else if (item.type === 7) {
                this.Box7.push(item)
              } else if (item.type === 8) {
                this.Box8.push(item)
              } else if (item.type === 9) {
                this.Box9.push(item)
              }
            })
          }
        })
      },
      roleListEvent(list) {
      },

      listEvent(list) {
      },

      //编辑权限确定事件
      submitAddRole() {
        http('post', 'permission/give_rights_org', {
          dirRights: this.roleListBox.toString(),
          opeRights: this.checked.toString(),
          orgIds: this.orgId || this.orgIdsList
        }).then(res => {
          if (res.data.success) {
            this.$message({message: res.data.msg, type: 'success'});
            this.$router.push({path: '/container'});
          } else {
            this.$message({message: res.data.msg, type: 'warning'});
          }
        })

      },
      //查看权限
      seeqxEvent() {
        let obj = {
          applyType: 4,
          orgId: this.orgId,
        };
        http('post', 'permission/opera_org', {
          org: obj
        }).then(res => {
          if (res.data.success) {
            let data = res.data.data.org;
            if (data.dirRightsList) {
              data.dirRightsList.forEach(item => {
                this.roleListBox.push(item.id)
              })
            }
            if (data.opeRightsList) {
              data.opeRightsList.forEach(item => {
                this.checked.push(item.id)
              })
            }
          }
        })
      }

    },

  }
</script>

<style scoped>
  .box {
    background: #fff;
    height: 100%;
    padding: 0 30px 50px 30px;
  }

  .p_title {
    padding: 10px 40px 10px 20px;
    overflow: hidden;
    margin: 0;
  }

</style>
